package sg.edu.nus.iss.cats.javabean;

import sg.edu.nus.iss.cats.model.*;
import sg.edu.nus.iss.cats.controller.*;
import sg.edu.nus.iss.cats.dataaccess.DAOException;

public class UsersBean{


    public String getRolesMenu(User user){
    
        //Get all roles
        //Load the roles from DB
        Role[] allRoles = null;
        try {
            UserManager userManager = new UserManager();
            allRoles = userManager.findAllRoles();
         }catch (DAOException e){
            System.out.println(
              "Sorry, an error occurred getting all roles");
        }       

    
        //Create the select menu to be displayed in the HTML form
        //Loop around all roles and display the pull down menu list for user    
        String menu=null;

        Role[] roles = user.getRoles();

        for (int j=0;j<allRoles.length;j++){
            menu += "<option value=\"" + allRoles[j].getName() +"\""; 
            boolean roleFlag = false;
            for (int i=0;i<roles.length;i++){
                if (roles[i].getName().equalsIgnoreCase(allRoles[j].getName())){ 
                    roleFlag = true;
                    break;
                }                      

            }

            if (roleFlag){
                //option box is selected 
                menu += " selected=\"selected\" >" + allRoles[j].getName() + "</option>\n"; 

            }else{
                menu += ">" + allRoles[j].getName() + "</option>\n";
            }
        }
            return menu;
    }
        
        
    public User getUserInfo(String userId){        
        //Get details of user
        User user = null;
        try {
            //Get all users details from DB and display
            UserManager userManager = new UserManager();
            user = userManager.findUser(userId);
        }catch (DAOException e){
            System.out.println(                 "Sorry, an error occurred getting the user details");
        }  

        return user;
    }
    
}